package com.digua.base.util

import android.Manifest
import android.os.Build
import androidx.annotation.StringRes
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.PermissionUtils.FullCallback
import com.digua.base.R

class PerUtil {

    companion object {
        fun getBluetooth(runnable: Runnable, fail: Runnable? = null) {
            permission({
                permission({
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                        permission(runnable, "", fail, Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_ADVERTISE, Manifest.permission.BLUETOOTH_SCAN)
                    } else {
                        runnable.run()
                    }
                }, "", fail, Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN)
            }, "", fail, Manifest.permission.ACCESS_FINE_LOCATION)
        }

        fun isBluetooth(): Boolean {
            if (PermissionUtils.isGranted(Manifest.permission.ACCESS_FINE_LOCATION)) {
                //获取蓝牙动态权限
                val permissions = arrayOf(Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN)
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                    permissions.plus(Manifest.permission.BLUETOOTH_SCAN)
                    permissions.plus(Manifest.permission.BLUETOOTH_CONNECT)
                    permissions.plus(Manifest.permission.BLUETOOTH_ADVERTISE)
                }
                return PermissionUtils.isGranted(*permissions)
            }
            return false
        }

        fun getBleConnect(runnable: Runnable) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                permission(runnable, R.string.permission_bluetooth_fail, null, Manifest.permission.BLUETOOTH_CONNECT)
            } else {
                runnable.run()
            }
        }

        fun getAlbum(fail: Runnable? = null, success: Runnable) {
            permission(success, R.string.permission_media_images_fail, fail, Manifest.permission.READ_MEDIA_IMAGES)
        }

        fun getStorage(fail: Runnable? = null, success: Runnable) {
            permission(success, R.string.permission_storage_fail, fail, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
        }

        fun getCAMERA(runnable: Runnable, fail: Runnable? = null) {
            permission(runnable, R.string.permission_camera_fail, fail, Manifest.permission.CAMERA)
        }

        fun getCoarseLocation(runnable: Runnable) {
            permission(runnable, R.string.permission_location_fail, null, Manifest.permission.ACCESS_COARSE_LOCATION)
        }

        fun getWriteSettings(runnable: Runnable) {
            permission(runnable, R.string.permission_sys_setting_fail, runnable, Manifest.permission.WRITE_SETTINGS)
        }

        fun permission(success: Runnable?, @StringRes failMsgResId: Int? = null, fail: Runnable? = null, vararg permissions: String) {
            val failMsg = failMsgResId?.let { ActivityUtils.getTopActivity().getString(it) }
            permission(success, failMsg, fail, *permissions)
        }

        fun permission(success: Runnable?, failMsg: String? = null, fail: Runnable? = null, vararg permissions: String) {
            if (PermissionUtils.isGranted(*permissions)) {
                success?.run()
            } else {
                PermissionUtils.permission(*permissions).callback(object : FullCallback {
                    override fun onGranted(granted: List<String>) {
                        success?.run()
                    }

                    override fun onDenied(deniedForever: List<String>, denied: List<String>) {
                        failMsg?.let { Toasts.showLong(it) }
                        fail?.run()
                    }
                }).request()
            }
        }
    }
}